import Vue from 'vue'
import Vuex from 'vuex'

import vuexPersist from "vuex-persist";

import http from '../http/http.js';

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    todolist: [],
  },
  mutations: {
    setlist(state, obj) {
      if (localStorage.getItem("vuex")) {
        state.todolist = JSON.parse(localStorage.getItem("vuex")).todolist;
      } else {
        state.todolist = obj;
      }
    },
    app(state, data) {
      state.todolist.push({
        _id: state.todolist.length + 1,
        Time: "",
        title: data,
        status: 0,
      })
    },
    remove(state, id) {
      state.todolist.forEach((item, index) => {
        if (item._id == id) {
          state.todolist.splice(index, 1);
        }
      })
    },
    qu(state, id) {
      state.todolist.forEach((item, index) => {
        if (item._id == id) {
          state.todolist[index].status = 2;
        }
      })
    },
    hf(state, id) {
      state.todolist.forEach((item, index) => {
        if (item._id == id) {
          state.todolist[index].status = 0;
        }
      })
    },
    t0(state, id) {
      state.todolist.forEach((item, index) => {
        if (item._id == id) {
          state.todolist[index].status = 1;
          state.todolist[index].Time = new Date().toLocaleDateString();

        }
      })
    },
    t1(state, id) {
      state.todolist.forEach((item, index) => {
        if (item._id == id) {
          state.todolist[index].status = 0;
        }
      })
    }
  },
  actions: {
    getlist(context) {
      http('/tolist.json', 'get').then((res) => {
        console.log(res);
        context.commit('setlist', res.data.data);
      })
    }
  },
  modules: {},
  plugins: [
    new vuexPersist({
      storage: window.localStorage,
    }).plugin,
  ]
})